﻿using RTracker.Models.Common;
using RTracker.Models.User;
using RTracker.Areas.Recruitment.Models.Vacancy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RTracker.Areas.Recruitment.Models.VacancyExpirence;
using RTracker.Areas.Recruitment.Models.VacancySalary;
using RTracker.Areas.Recruitment.Models.VacancyStatus;
using RTracker.Areas.Recruitment.Models.Shift;
using RTracker.Areas.Recruitment.Models.Department;
using RTracker.Areas.Recruitment.Models.Client;

namespace RTracker.Areas.Recruitment.Controllers
{
    public class VacancyController : RTracker.Controllers.AppBaseController
    {
        //
        // GET: /Vacancy/


        [HttpPost]
        public ActionResult GridRefresh(int pnId, string pstrSortOn, string pstrSortType, int pnPageNo, int pnPageSize, string pstrSearch, string pstrSearchOn, int pnOrgId = 1)
        {
            GVacancy objGVacancy = new GVacancy();
            try
            {
                if (pstrSearch == null || pstrSearchOn == null)
                {
                    pstrSearch = "";
                    pstrSearchOn = "";
                }

                //call CheckSession to get session status
                string SessionState = CheckSessionState();

                //check if session state is ok else return false status to view.
                if (SessionState == CAppConstants.SessionSuccess)
                {
                    CVacancy objCVacancy = new CVacancy() { VacancyId = pnId, SortOn = pstrSortOn, SortType = pstrSortType, PageNumber = pnPageNo, RowSize = pnPageSize, Search = pstrSearch, SearchOn = pstrSearchOn, OrganizationId = pnOrgId };


                    //check if current Vacancys organization id is 1 then it means it is superVacancy then need to get all organizations list
                    if (CurrentUserSession.OrganizationId == pnOrgId)
                    {

                        MVacancy objMVacancy = new MVacancy();

                        objGVacancy = objMVacancy.GetVacancyGridList(objCVacancy, AuthenticationContext);

                        if (objGVacancy.LstVacancy != null)
                        {
                            if (objGVacancy.LstVacancy.Count > 0)
                            {
                                objGVacancy.LstVacancy.ForEach(Vacancy =>
                                {
                                    Vacancy.CreatedByName = GetFullName(Vacancy.CreatedBy);
                                    Vacancy.ModifiedByName = GetFullName(Vacancy.ModifiedBy);
                                    Vacancy.SoftLockedByName = GetFullName(Vacancy.SoftLockedBy);
                                    Vacancy.HardLockedByName = GetFullName(Vacancy.HardLockedBy);
                                });
                            }
                        }

                        // if status is true get object of the record to be selected in the list.
                        // first of all we will check the if given record exist in the list.
                        // if given record exist then get the object of that record.
                        // if record not exist in the list then select the first record of the list.
                        if (objGVacancy.Status.Success)
                        {
                            // if status is true get object of the record to be selected in the list.
                            // first of all we will check the if given record exist in the list.
                            // if given record exist then get the object of that record.
                            // if record not exist in the list then select the first record of the list.
                            if (objGVacancy.LstVacancy != null)
                            {
                                CVacancy objSelectedCVacancy = objGVacancy.LstVacancy.Where(m => m.VacancyId == pnId).DefaultIfEmpty(objGVacancy.LstVacancy[0]).First();

                                // check if record is not already hard locked or softlocked
                                if (objSelectedCVacancy.IsSoftLocked == false && objSelectedCVacancy.IsHardLocked == false)
                                {
                                    objSelectedCVacancy.CreatedBy = CurrentUserSession.UserId;
                                    objGVacancy.Status = objMVacancy.SetVacancySoftLock(AuthenticationContext, objSelectedCVacancy);
                                    if (objGVacancy.Status.Success)
                                    {
                                        // updating the list with softlock details
                                        objGVacancy.LstVacancy.Where(m => m.VacancyId == objSelectedCVacancy.VacancyId).First().IsSoftLocked = true;
                                        objGVacancy.LstVacancy.Where(m => m.VacancyId == objSelectedCVacancy.VacancyId).First().SoftLockedBy = CurrentUserSession.UserId;
                                    }
                                }
                            }
                        }

                        ViewBag.bPagingEnabled = true;
                        ViewBag.bSearchEnabled = true;
                        ViewBag.bNewDeleteEnabled = true;
                        ViewBag.bCopyPasteEnabled = false;
                    }
                    //else get detail for current Vacancys organization and show it in grid.
                    else
                    {
                        objCVacancy = new CVacancy() { OrganizationId = CurrentUserSession.OrganizationId };

                        MVacancy objMVacancy = new MVacancy();

                        objGVacancy.Status = objMVacancy.GetVacancyDetail(AuthenticationContext, ref objCVacancy);

                        objGVacancy.LstVacancy.Add(objCVacancy);
                        //set view bag variables for pagging, new, delete and search.
                        ViewBag.bPagingEnabled = false;
                        ViewBag.bSearchEnabled = false;
                        ViewBag.bNewDeleteEnabled = false;
                        ViewBag.bCopyPasteEnabled = false;
                    }

                    // check if status is true
                    if (objGVacancy.Status.Success)
                    {
                        if (objGVacancy.LstVacancy != null)
                        {
                            if (objGVacancy.LstVacancy.Count > 0)
                            {
                                objGVacancy.LstVacancy[0].SortOn = pstrSortOn;
                                objGVacancy.LstVacancy[0].SortType = pstrSortType;
                            }
                        }
                        // set the values of sort on and sort type in the view bag.
                        ViewBag.SortType = pstrSortType;
                        ViewBag.SortOn = pstrSortOn;
                        ViewBag.CurrentUser = CurrentUserSession.UserId;
                    }
                }
                else
                {
                    //set status to false and its detail.
                    objGVacancy.Status = new CStatus();
                    objGVacancy.Status.Success = false;
                    objGVacancy.Status.StatusDetails = SessionState;
                }
            }
            catch (Exception ex)
            {
                // catch the exceptions in any step ,if any 
                //set status to false and its detail as exception message.
                objGVacancy.Status = new CStatus();
                objGVacancy.Status.Success = false;
                objGVacancy.Status.StatusDetails = ex.Message;
            }
            ViewBag.CurrentUser = CurrentUserSession.UserId;
            //check if status is true then return list view(partial view of list) else return object with false status.
            if (objGVacancy.Status.Success)
                return PartialView("_list", objGVacancy);
            else
                return Json(objGVacancy);
        }

        [HttpPost]
        public ActionResult GetDetails(int pnId)
        {
            GVacancy objGVacancy = new GVacancy();

            objGVacancy.Status = new CStatus();

            objGVacancy.Vacancy = new CVacancy();

            objGVacancy.Vacancy.VacancyId = pnId;

            CVacancy objCVacancy = new CVacancy();

            ViewBag.IsDisabled = true;
            try
            {
                //call CheckSession to get session status
                string SessionState = CheckSessionState();

                //IsDisabled checks whether cureently selected row is hardlocked or softlocked by other
                ViewBag.IsDisabled = true;
                ViewBag.IsNew = false;
                //check if session state is ok else return false status to view.
                if (SessionState == CAppConstants.SessionSuccess)
                {
                    MVacancyExpirence objMVacancyExpirence = new MVacancyExpirence();
                    MVacancySalary objMVacancySalary = new MVacancySalary();
                    MVacancyStatus objMVacancyStatus = new MVacancyStatus();
                    MShift objMShift = new MShift();
                    MDepartment objMDepartment = new MDepartment();
                    MClient objMClient = new MClient();

                    // get vacancy expirence list
                    List<CVacancyExpirence> tempListVacancyExpirence = new List<CVacancyExpirence>();
                    objGVacancy.Status = objMVacancyExpirence.GetVacancyExpirenceList(AuthenticationContext, CurrentUserSession.OrganizationId, out tempListVacancyExpirence);
                    objGVacancy.LstVacancyExpirence = new List<CVacancyExpirence>();
                    objGVacancy.LstVacancyExpirence.AddRange(tempListVacancyExpirence);

                    // get vacancy salary list
                    List<CVacancySalary> tempListVacancySalary = new List<CVacancySalary>();
                    objGVacancy.Status = objMVacancySalary.GetVacancySalaryList(AuthenticationContext, CurrentUserSession.OrganizationId, out tempListVacancySalary);
                    objGVacancy.LstVacancySalary = new List<CVacancySalary>();
                    objGVacancy.LstVacancySalary.AddRange(tempListVacancySalary);

                    // get vacancy Status list
                    List<CVacancyStatus> tempListVacancyStatus = new List<CVacancyStatus>();
                    objGVacancy.Status = objMVacancyStatus.GetVacancyStatusList(AuthenticationContext, CurrentUserSession.OrganizationId, out tempListVacancyStatus);
                    objGVacancy.LstVacancyStatus = new List<CVacancyStatus>();
                    objGVacancy.LstVacancyStatus.AddRange(tempListVacancyStatus);

                    // get Shift list
                    List<CShift> tempListShift = new List<CShift>();
                    objGVacancy.Status = objMShift.GetShiftList(AuthenticationContext, CurrentUserSession.OrganizationId, out tempListShift);
                    objGVacancy.LstShift = new List<CShift>();
                    objGVacancy.LstShift.AddRange(tempListShift);

                    // get Department list
                    List<CDepartment> tempListDepartment = new List<CDepartment>();
                    objGVacancy.Status = objMDepartment.GetDepartmentList(AuthenticationContext, CurrentUserSession.OrganizationId, out tempListDepartment);
                    objGVacancy.LstDepartment = new List<CDepartment>();
                    objGVacancy.LstDepartment.AddRange(tempListDepartment);

                    // get Client list
                    List<CClient> tempListClient = new List<CClient>();
                    objGVacancy.Status = objMClient.GetClientList(AuthenticationContext, CurrentUserSession.OrganizationId, out tempListClient);
                    objGVacancy.LstClient = new List<CClient>();
                    objGVacancy.LstClient.AddRange(tempListClient);

                    //check if Id param is 0 means new is clicked then set selected row object to new.
                    if (0 == pnId)
                    {
                        if (checkUserPermission(CUserPermissionsConstants.OrganizationCreate))
                        {
                            //here we will append new form to create new organisation
                            ViewBag.IsNew = true;
                            objGVacancy.Status.Success = true;
                            objGVacancy.Status.StatusDetails = CAppConstants.DetailSuccess;
                        }
                        else
                        {
                            objGVacancy.Status.Success = false;
                            objGVacancy.Status.StatusDetails = CAppErrorsConstants.NoPermission;
                        }
                    }
                    else
                    {
                        if (checkUserPermission(CUserPermissionsConstants.OrganizationDetailView))
                        {
                            //here we will append new for detail 
                            MVacancy objMVacancy = new MVacancy();

                            ViewBag.IsNew = false;

                            objCVacancy.VacancyId = objGVacancy.Vacancy.VacancyId;

                            objGVacancy.Status = objMVacancy.GetVacancyDetail(AuthenticationContext, ref objCVacancy);

                            //get the different admin names from their id from the application Vacancy list
                            objCVacancy.CreatedByName = GetFullName(objCVacancy.CreatedBy);
                            objCVacancy.ModifiedByName = GetFullName(objCVacancy.ModifiedBy);
                            objCVacancy.SoftLockedByName = GetFullName(objCVacancy.SoftLockedBy);
                            objCVacancy.HardLockedByName = GetFullName(objCVacancy.HardLockedBy);


                            if (!CheckLocksByOther(objCVacancy))
                            {
                                ViewBag.IsDisabled = false;
                            }
                            if (objGVacancy.Status.Success == true)
                            {
                                objGVacancy.Vacancy = objCVacancy;
                            }
                        }
                        else
                        {
                            objGVacancy.Status = new CStatus();
                            objGVacancy.Status.Success = false;
                            objGVacancy.Status.StatusDetails = CAppErrorsConstants.NoPermission;
                        }
                    }
                }
                else
                {
                    objGVacancy.Status = new CStatus();
                    objGVacancy.Status.Success = false;
                    objGVacancy.Status.StatusDetails = SessionState;
                }
            }
            catch (Exception ex)
            {

                objGVacancy.Status = new CStatus();
                objGVacancy.Status.Success = false;
                objGVacancy.Status.StatusDetails = ex.Message;
            }

            return PartialView("_detail", objGVacancy);
        }

        [HttpPost]
        public ActionResult Save(GVacancy oGVacancy)
        {
            try
            {
                //check if Model validation is true if not then return detail view with errors
                if (true)//ModelState.IsValid
                {
                    //call CheckSession to get session status
                    string SessionState = CheckSessionState();
                    //check if session state is ok else return false status to view.
                    if (SessionState == CAppConstants.SessionSuccess)
                    {
                        bool _userPermission = false;

                        if (oGVacancy.Vacancy.OrganizationId == 0)
                        {
                            _userPermission = checkUserPermission(CUserPermissionsConstants.OrganizationCreate);
                        }
                        else
                        {
                            _userPermission = checkUserPermission(CUserPermissionsConstants.OrganizationUpdate);
                        }


                        if (_userPermission)
                        {
                            CVacancy objCVacancy = new CVacancy();
                            objCVacancy = oGVacancy.Vacancy;
                            MVacancy objMVacancy = new MVacancy();

                            if (objCVacancy.VacancyId == 0)
                            {
                                objCVacancy.OrganizationId = CurrentUserSession.OrganizationId;
                                oGVacancy.Status = objMVacancy.CreateVacancy(AuthenticationContext, ref objCVacancy);
                            }
                            else
                            {
                                objCVacancy.OrganizationId = CurrentUserSession.OrganizationId;
                                oGVacancy.Status = objMVacancy.UpdateVacancy(AuthenticationContext, ref objCVacancy);
                            }
                        }
                        else
                        {
                            oGVacancy.Status = new CStatus();
                            oGVacancy.Status.Success = false;
                            oGVacancy.Status.StatusDetails = CAppErrorsConstants.NoPermission;
                        }
                    }
                    else
                    {
                        oGVacancy.Status = new CStatus();
                        oGVacancy.Status.Success = false;
                        oGVacancy.Status.StatusDetails = SessionState;
                    }
                }
                else
                {
                    oGVacancy.Status = new CStatus();
                    oGVacancy.Status.Success = false;
                    oGVacancy.Status.StatusDetails = CAppErrorsConstants.ValidationFail;
                }
            }
            catch (Exception ex)
            {
                oGVacancy.Status = new CStatus();
                oGVacancy.Status.Success = false;
                oGVacancy.Status.StatusDetails = ex.Message;
            }
            return Json(oGVacancy);
        }

        [HttpPost]
        public ActionResult DeleteRecords(string pstrIds)
        {
            GVacancy objGVacancy = new GVacancy();

            objGVacancy.Vacancy = new CVacancy();

            objGVacancy.Vacancy.VacancyIds = pstrIds;
            try
            {
                //call CheckSession to get session status
                string SessionState = CheckSessionState();

                //IsDisabled checks whether cureently selected row is hardlocked or softlocked by other
                ViewBag.IsDisabled = true;
                ViewBag.IsNew = false;

                //check if session state is ok else return false status to view.
                if (SessionState == CAppConstants.SessionSuccess)
                {
                    //check if Id param is 0 means new is clicked then set selected row object to new.
                    if (pstrIds != "")
                    {
                        if (checkUserPermission(CUserPermissionsConstants.OrganizationDelete))
                        {
                            //code to delete the record
                            MVacancy objMVacancy = new MVacancy();

                            CVacancy objCVacancy = new CVacancy();

                            objCVacancy.VacancyIds = objGVacancy.Vacancy.VacancyIds;

                            objGVacancy.Status = objMVacancy.DeleteVacancy(AuthenticationContext, ref objCVacancy);

                            if (objGVacancy.Status.Success == true)
                            {
                                objGVacancy.Vacancy = objCVacancy;
                            }
                        }
                        else
                        {
                            objGVacancy.Status = new CStatus();
                            objGVacancy.Status.Success = false;
                            objGVacancy.Status.StatusDetails = CAppErrorsConstants.NoPermission;
                        }
                    }
                    else
                    {
                        objGVacancy.Status = new CStatus();
                        objGVacancy.Status.Success = false;
                        objGVacancy.Status.StatusDetails = CAppErrorsConstants.NotValidRequest;
                    }
                }
                else
                {
                    objGVacancy.Status = new CStatus();
                    objGVacancy.Status.Success = false;
                    objGVacancy.Status.StatusDetails = SessionState;
                }
            }
            catch (Exception ex)
            {

                objGVacancy.Status = new CStatus();
                objGVacancy.Status.Success = false;
                objGVacancy.Status.StatusDetails = ex.Message;
            }

            return Json(objGVacancy.Status);
        }

        [HttpPost]
        public ActionResult ReleaseSoftLock(int pnId)
        {
            GVacancy objGVacancy = new GVacancy();

            objGVacancy.Vacancy = new CVacancy();

            objGVacancy.Vacancy.VacancyId = pnId;
            try
            {
                //call CheckSession to get session status
                string SessionState = CheckSessionState();
                //IsDisabled checks whether cureently selected row is hardlocked or softlocked by other
                ViewBag.IsDisabled = true;
                ViewBag.IsNew = false;
                //check if session state is ok else return false status to view.
                if (SessionState == CAppConstants.SessionSuccess)
                {

                    //check if Id param is 0 means new is clicked then set selected row object to new.
                    if (0 == pnId)
                    {
                        if (checkUserPermission(CUserPermissionsConstants.OrganizationUpdate))
                        {
                            //here we will append new form to create new organisation
                            ViewBag.IsNew = true;
                            objGVacancy.Status.Success = true;
                        }
                        else
                        {
                            objGVacancy.Status.Success = false;
                            objGVacancy.Status.StatusDetails = CAppErrorsConstants.NoPermission;
                        }
                    }
                    else
                    {
                        MVacancy objMVacancy = new MVacancy();
                        objGVacancy.Vacancy.CreatedBy = CurrentUserSession.UserId;
                        objGVacancy.Status = objMVacancy.ReleaseVacancySoftLock(AuthenticationContext, objGVacancy.Vacancy);
                    }
                }
                else
                {
                    objGVacancy.Status = new CStatus();
                    objGVacancy.Status.Success = false;
                    objGVacancy.Status.StatusDetails = SessionState;
                }
            }
            catch (Exception ex)
            {

                objGVacancy.Status = new CStatus();
                objGVacancy.Status.Success = false;
                objGVacancy.Status.StatusDetails = ex.Message;
            }

            return Json(objGVacancy.Status);
        }

        [HttpPost]
        public ActionResult SetHardLock(int pnId)
        {
            GVacancy objGVacancy = new GVacancy();

            objGVacancy.Vacancy = new CVacancy();

            objGVacancy.Vacancy.VacancyId = pnId;
            try
            {
                //call CheckSession to get session status
                string SessionState = CheckSessionState();
                //IsDisabled checks whether cureently selected row is hardlocked or softlocked by other
                ViewBag.IsDisabled = true;
                ViewBag.IsNew = false;
                //check if session state is ok else return false status to view.
                if (SessionState == CAppConstants.SessionSuccess)
                {

                    ////check if Id param is 0 means new is clicked then set selected row object to new.
                    //if (0 == pnId)
                    //{
                    //    if (checkUserPermission(CUserPermissionsConstants.OrganizationUpdate))
                    //    {
                    //        //here we will append new form to create new organisation
                    //        ViewBag.IsNew = true;
                    //        objGVacancy.Status.Success = true;
                    //    }
                    //    else
                    //    {
                    //        objGVacancy.Status.Success = false;
                    //        objGVacancy.Status.StatusDetails = CAppErrorsConstants.NoPermission;
                    //    }
                    //}
                    //else
                    //{
                    MVacancy objMVacancy = new MVacancy();
                    objGVacancy.Vacancy.CreatedBy = CurrentUserSession.UserId;
                    objGVacancy.Status = objMVacancy.SetVacancyHardLock(AuthenticationContext, objGVacancy.Vacancy);
                    //}
                }
                else
                {
                    objGVacancy.Status = new CStatus();
                    objGVacancy.Status.Success = false;
                    objGVacancy.Status.StatusDetails = SessionState;
                }
            }
            catch (Exception ex)
            {

                objGVacancy.Status = new CStatus();
                objGVacancy.Status.Success = false;
                objGVacancy.Status.StatusDetails = ex.Message;
            }

            return Json(objGVacancy.Status);
        }

        [HttpPost]
        public ActionResult ReleaseHardLock(int pnId)
        {
            GVacancy objGVacancy = new GVacancy();

            objGVacancy.Vacancy = new CVacancy();

            objGVacancy.Vacancy.VacancyId = pnId;
            try
            {
                //call CheckSession to get session status
                string SessionState = CheckSessionState();
                //IsDisabled checks whether cureently selected row is hardlocked or softlocked by other
                ViewBag.IsDisabled = true;
                ViewBag.IsNew = false;
                //check if session state is ok else return false status to view.
                if (SessionState == CAppConstants.SessionSuccess)
                {

                    ////check if Id param is 0 means new is clicked then set selected row object to new.
                    //if (0 == pnId)
                    //{
                    //    if (checkUserPermission(CUserPermissionsConstants.OrganizationUpdate))
                    //    {
                    //        //here we will append new form to create new organisation
                    //        ViewBag.IsNew = true;
                    //        objGVacancy.Status.Success = true;
                    //    }
                    //    else
                    //    {
                    //        objGVacancy.Status.Success = false;
                    //        objGVacancy.Status.StatusDetails = CAppErrorsConstants.NoPermission;
                    //    }
                    //}
                    //else
                    //{
                    MVacancy objMVacancy = new MVacancy();
                    objGVacancy.Vacancy.CreatedBy = CurrentUserSession.UserId;
                    objGVacancy.Status = objMVacancy.ReleaseVacancyHardLock(AuthenticationContext, objGVacancy.Vacancy);
                    //}
                }
                else
                {
                    objGVacancy.Status = new CStatus();
                    objGVacancy.Status.Success = false;
                    objGVacancy.Status.StatusDetails = SessionState;
                }
            }
            catch (Exception ex)
            {

                objGVacancy.Status = new CStatus();
                objGVacancy.Status.Success = false;
                objGVacancy.Status.StatusDetails = ex.Message;
            }

            return Json(objGVacancy.Status);
        }
    }
}
